package cn.lg.soar.system.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.database.util.MpSqlUtils;
import cn.lg.soar.database.util.PageUtils;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.auth.entity.Permit;
import cn.lg.soar.system.auth.service.IPermitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = "权限管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/permit")
public class PermitController extends BaseController<Permit, IPermitService> {

    @Override
    protected Serializable getId(Permit entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "权限";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated Permit permit) {
        return super.createResult(permit);
    }

    @Log(value = "修改" + SUBJECT, data = "#p0", dataId = "#p0.id")
    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated Permit permit) {
        return super.updateResult(permit);
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DetailMapping
    public HttpResult<Permit> detail(@Id Long id) {
        return super.detailResult(id);
    }

    @QueryMapping
    public HttpResult<PageResult<Permit>> query(PageQuery pageQuery, QueryParams params, Long[] menuIds){
        QueryWrapper<Permit> wrapper = new SqueryWapper<>(Permit.class).listWrapper(params);
        MpSqlUtils.handleInOrEq(menuIds, (sql, value) -> {
            wrapper.exists("select * from lg_sys_menu_2_permit m where lg_sys_permit.id=m.permit_id and m.menu_id " + sql, value);
        });
        IPage<Permit> page = service.page(PageUtils.toPage(pageQuery), wrapper);
        return HttpResult.data(PageUtils.toPageResult(page));
    }

    @ListMapping
    public HttpResult<List<Permit>> list(QueryParams params){
        return HttpResult.data(
                super.getList(params)
        );
    }

    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

    @Log(value = "导出" + SUBJECT, data = "#p0")
    @ExportMapping
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel(PermitController.SUBJECT, params, resp);
    }

    @Log(value = "导入" + SUBJECT)
    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ApiOperation("扫描生成权限")
    @PostMapping(value = "scan", name = "扫描生成权限")
    public HttpResult<Void> scan(){
        this.service.scanServletPathGenPermit();
        return HttpResult.success();
    }

    @ApiOperation("清空缓存")
    @PostMapping(value = "clear-cache", name = "清空缓存")
    public HttpResult<Void> clearCache(){
        this.service.clearCache();
        return HttpResult.success();
    }
}
